package org.jeecg.modules.controller.medicineOrder;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.doctor.entity.DoctorRegisterHospital;
import org.jeecg.common.modules.doctor.service.IDoctorRegisterHospitalService;
import org.jeecg.common.modules.medicineOrder.entity.OutOrder;
import org.jeecg.common.modules.medicineOrder.service.IOutOrderService;
import org.jeecg.common.modules.shippingAddress.entity.ShippingAddress;
import org.jeecg.common.modules.shippingAddress.service.IShippingAddressService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.UidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @Description: 订单表
* @Author: jeecg-boot
* @Date:   2022-03-02
* @Version: V1.0
*/
@Api(tags="订单表")
@RestController
@RequestMapping("api/v1/outOrder")
@Slf4j
public class OutOrderController extends JeecgController<OutOrder, IOutOrderService> {
   @Autowired
   private IOutOrderService outOrderService;
    @Autowired
    private IShippingAddressService shippingAddressService;
    @Resource
    private IDoctorRegisterHospitalService doctorRegisterHospitalService;
   /**
    * 分页列表查询
    *
    * @return
    */
   @AutoLog(value = "订单表-分页列表查询")
   @ApiOperation(value="订单表-分页列表查询", notes="订单表-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(@RequestParam(name="openid") String openid,
                                  @RequestParam(name="expressType") String expressType,
                                  @RequestParam(name="courierStatus") String courierStatus) {
       List<OutOrder> list = outOrderService.list(Wrappers.lambdaQuery(OutOrder.class).eq(OutOrder::getOpenid,openid).eq(expressType.length()>0&&"nner".equals(expressType),OutOrder::getExpressType,expressType).ne(expressType.length()>0&&!"nner".equals(expressType),OutOrder::getExpressType,"nner").eq(courierStatus.length()>0,OutOrder::getCourierStatus,courierStatus));
       if(list!=null){
           for (OutOrder order:list){
               if(order.getCourierNumber() == null){
                   continue;
               }
               String status = queryCourierStatus(order);
               order.setCourierType(status);
               outOrderService.getById(order);
           }
       }
       return Result.ok(list);
   }

   /**
    *   添加
    *
    * @param outOrder
    * @return
    */
   @AutoLog(value = "订单表-添加")
   @ApiOperation(value="订单表-添加", notes="订单表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody OutOrder outOrder) {
       String id = UidUtils.getUid32();
       outOrder.setId(id);
       outOrderService.save(outOrder);
       return Result.ok(outOrder);
   }

   /**
    *  编辑
    *
    * @param outOrder
    * @return
    */
   @AutoLog(value = "订单表-编辑")
   @ApiOperation(value="订单表-编辑", notes="订单表-编辑")
   @PostMapping(value = "/edit")
   public Result<?> edit(@RequestBody OutOrder outOrder) {
       outOrderService.updateById(outOrder);
       return Result.ok("成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "订单表-通过id删除")
   @ApiOperation(value="订单表-通过id删除", notes="订单表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       outOrderService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "订单表-批量删除")
   @ApiOperation(value="订单表-批量删除", notes="订单表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.outOrderService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "订单表-通过id查询")
   @ApiOperation(value="订单表-通过id查询", notes="订单表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       OutOrder outOrder = outOrderService.getById(id);
       if(outOrder==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(outOrder);
   }

    /**
     * 收货人地址查询
     *
     * @return
     */
    @AutoLog(value = "收货人地址查询")
    @ApiOperation(value="收货人地址查询", notes="收货人地址查询")
    @GetMapping(value = "/queryReceivedAddress")
    public Result<?> queryReceivedAddress(@RequestParam(name="KDid",required=true) String KDid) {
        ShippingAddress shippingAddressList = shippingAddressService.getById(KDid);
        return Result.ok(shippingAddressList);
    }

    /**
     * 收货人地址查询
     *
     * @return
     */
    @AutoLog(value = "发货人地址查询")
    @ApiOperation(value="发货人地址查询", notes="发货人地址查询")
    @GetMapping(value = "/querySendAddress")
    public Result<?> querySendAddress(@RequestParam(name="hospitalId",required=true) String hospitalId) {
        DoctorRegisterHospital hospital = doctorRegisterHospitalService.getById(hospitalId);
        return Result.ok(hospital);
    }
    /**
     * 获取 所有医院地址
     *
     * @return
     */
    @AutoLog(value = "获取 所有医院地址")
    @ApiOperation(value="获取 所有医院地址", notes="获取 所有医院地址")
    @GetMapping(value = "/queryHospitalList")
    public Result<?> queryHospitalList() {
        List<DoctorRegisterHospital> list = doctorRegisterHospitalService.list();
        return Result.ok(list);
    }

    /**
     * 处方号查询 并返回 处方信息
     *
     * @return
     */
    @AutoLog(value = "处方号查询 并返回 处方信息")
    @ApiOperation(value="处方号查询 并返回 处方信息", notes="处方号查询 并返回 处方信息")
    @PostMapping(value = "/queryOutNo")
    public Result<?> queryOutNo(@RequestParam("outNO") String outNo) {
        //todo 查询制药系统 处方号
        return Result.ok();
    }
    /**
     * 计算药剂总重量
     *
     * @return
     */
    @AutoLog(value = "计算药剂总重量")
    @ApiOperation(value="计算药剂总重量", notes="计算药剂总重量")
    @PostMapping(value = "/cakcykateWeight")
    public Result<?> cakcykateWeight(@RequestBody OutOrder outOrder) {
        //todo 查询制药系统 处方号
        return Result.ok();
    }
    /**
     *  订单选择快递
     *
     * @param outOrder
     * @return
     */
    @AutoLog(value = "订单选择快递")
    @ApiOperation(value="订单选择快递", notes="订单选择快递")
    @PostMapping(value = "/chooseKuaiDi")
    public Result<?> chooseKuaiDi(@RequestBody OutOrder outOrder) {
        outOrderService.updateById(outOrder);
        return Result.ok("编辑成功!");
    }

    /**
     *  订单选择快递列表
     * @return
     */
    @AutoLog(value = "订单选择快递列表")
    @ApiOperation(value="订单选择快递列表", notes="订单选择快递列表")
    @PostMapping(value = "/queryKuaiDiList")
    public Result<?> queryKuaiDiList() {
        //todo 获取制药系统快递列表
        //暂时
        List<CourierVO> courierVOS = new ArrayList<>();
        CourierVO courierVO = new CourierVO();
        courierVO.setCourierNumber("1");
        courierVO.setCourierName("京东快递");
        courierVO.setForecastAmount(2.00);
        courierVO.setArriveTime(new Date());
        courierVOS.add(courierVO);
        CourierVO courierVO1 = new CourierVO();
        courierVO1.setCourierNumber("2");
        courierVO1.setCourierName("顺风快递");
        courierVO1.setForecastAmount(4.00);
        courierVO1.setArriveTime(new Date());
        courierVOS.add(courierVO1);
        CourierVO courierVO2 = new CourierVO();
        courierVO2.setCourierNumber("3");
        courierVO2.setCourierName("百世快递");
        courierVO2.setForecastAmount(5.00);
        courierVO2.setArriveTime(new Date());
        courierVOS.add(courierVO2);
        return Result.ok(courierVOS);
    }
    /**
     *  明细
     *
     * @return
     */
    @AutoLog(value = "明细")
    @ApiOperation(value="明细", notes="明细")
    @PostMapping(value = "/orderDetails")
    public Result<?> orderDetails(@RequestBody OutOrderVO outOrderVO) {
        OrderDeatilsVO orderDeatilsVO = new OrderDeatilsVO();
        OutOrder order = outOrderService.getById(outOrderVO.getId());
        orderDeatilsVO.setId(outOrderVO.getId());
        orderDeatilsVO.setCount(order.getCount());
        orderDeatilsVO.setItemType("中草药");
        orderDeatilsVO.setWeight(order.getWeight());
        ShippingAddress shippingAddressList = shippingAddressService.getById(outOrderVO.getKDid());
        orderDeatilsVO.setEndPoint(shippingAddressList!= null ?shippingAddressList.getCity():"");
        orderDeatilsVO.setStartPoint("北京市");
        return Result.ok(orderDeatilsVO);
    }

    /**
     * 更新快递状态
     */
    private String queryCourierStatus(OutOrder order){

        //TODO 调用制药系统 获取快递状态信息
        return "已发货";
    }


    /**
     *  明细
     *
     * @param outOrder
     * @return
     */
    @AutoLog(value = "明细")
    @ApiOperation(value="明细", notes="明细")
    @PostMapping(value = "/orderCourierDetails")
    public Result<?> orderCourierDetails(@RequestBody OutOrder outOrder) {
        OrderDeatilsVO orderDeatilsVO = new OrderDeatilsVO();
        OutOrder order = outOrderService.getById(outOrder.getId());
        orderDeatilsVO.setId(outOrder.getId());
        orderDeatilsVO.setAmount(order.getAmount());
        orderDeatilsVO.setPackAmount(5.00);
        orderDeatilsVO.setCourierAmount(2.00);
        orderDeatilsVO.setShangmenAmount(4.00);
        return Result.ok(orderDeatilsVO);
    }
}
